package com.kusiri.kframework.metadata
{
	import com.kusiri.kframework.KfContext;
	import com.kusiri.kframework.component.IKfComponent;
	import com.kusiri.kframework.ui.KfUiComponent;
	import com.kusiri.kframework.ui.KfUiEventActions;
	
	public class KfComponentConfiguration
	{
		protected var _propertyConfigurations : Array;
		protected var _eventActions : KfUiEventActions;
		protected var _id : String;
		protected var _name : String;
		protected var _clazz : String;
		protected var _appconfig : KfApplicationConfiguration;
		
		public function KfComponentConfiguration( id : String, name : String, clazz : String, appconfig : KfApplicationConfiguration)
		{
			_id = id;
			_name = name;
			_clazz = clazz;
			_appconfig = appconfig;
			_propertyConfigurations = [];

		}
		
		public function toXml() : XML
		{
			var xml : XML = <component id={id} name={name} clazz={clazz}></component>;
			
			for each ( var conf : KfComponentPropertyConfiguration in _propertyConfigurations )
			{
				xml.appendChild(conf.toXml());
			}
			
			return xml;
		}
		
		public static function fromXml(appConfig:KfApplicationConfiguration, component:XML) : KfComponentConfiguration
		{
			var componentConfiguration : KfComponentConfiguration = new KfComponentConfiguration(component.@id, component.@name, component.@clazz, appConfig);
			
			for each ( var property : XML in component.property )
			{
				componentConfiguration.setProperty(property.@name, KfComponentPropertyConfiguration.fromXml(componentConfiguration, property));
			}
			
//			_eventActions = new KfUiEventActions();
//
//			for each (var event : XML in xmlConfiguration.event)
//			{
//				var actions : Array = [];
//				for each (var action : XML in event.action)
//				{
//					var eventAction : KfUiEventAction = new KfUiEventAction();
//					eventAction.clazz = action.@clazz;
//					var propertyMap : Object = new Object();
//					eventAction.propertyMap = propertyMap;
//					for each (var aproperty : XML in action.property)
//					{
//						propertyMap[aproperty.@name] = aproperty.@value ? aproperty.@value : aproperty.toString();
//					}
//					actions.push(eventAction);
//				}
//				_eventActions[event.@name] = actions;
//			} 

			return componentConfiguration;
		}
		
		public function setProperty( name : String, configuration : KfComponentPropertyConfiguration ) : void
		{
			_propertyConfigurations[name] = configuration;
		}
		
		public function get applicationConfiguration() : KfApplicationConfiguration
		{
			return _appconfig;
		}
		
		public function get clazz() : String
		{
			return _clazz;
		}
		
		public function get name() : String
		{
			return _name;
		}

		public function get id() : String
		{
			return _id;
		}
		
		public function getPropertyConfiguration( propertyName : String ) : KfComponentPropertyConfiguration
		{
			return _propertyConfigurations[propertyName];
		}
		
		public function get metadata() : KfComponentMetadata
		{
			return KfComponentMetadataLocator.getComponentMetadata(_clazz);
		}

		public function getNewInstance(context : KfContext) : IKfComponent
		{
			var component : IKfComponent  = metadata.getNewInstance();

			for each (var property : KfComponentPropertyMetadata in metadata.settableProperties)
			{
				var configuration : KfComponentPropertyConfiguration = getPropertyConfiguration(property.name);
				if ( configuration == null ) continue;
				component[property.name] = property.getPropertyValueFromConfiguration(context, configuration);
			} 

			if ( metadata.isUiComponent )
			{
				var uiComponent : KfUiComponent = component as KfUiComponent;
				uiComponent.__eventActions = _eventActions;
			}
			
			return component;
		}

	}
}